;*****************************************************************************************************
;+
; NAME:
;
;      eOPLOT (enhanced OPLOT)
;
; PURPOSE:
;
;      This is the enhanced version of the standard IDL OPLOT
;      command. It uses the same syntax and support most of the IDL
;      plotting keywords. The output plot is displayed in an IGL window
;      so that you can easily interact with it.
;
; CALLING SEQUENCE:
;
;      eOPLOT, x, y
;
; ARGUMENTS:
;
;     X:            The independent variable used to produce the
;                   plot. If not provided, set to FINDGEN(N_Elements(Y)).
;
;     Y:            The dependent variable used to produce the plot.
;
;                 
; KEYWORD PARAMETERS:
;
;     AUTOSCALE:    *IGL ADDITION * : Set this keyword to have the
;                   axis coordinate system on which the plot is
;                   overplotted to be autoscaled.
;
;     COLOR:        The name of the foreground color for the plot. The
;                   default is !IGL_P.color
;
;     LINESTYLE:    The style for the plot line (integer). The default
;                   is  !IGL_P.linestyle
;
;     NOCLIP:       If set, prevent the clipping at the edge of the
;                   plot window. The default is !IGL_P.noclip
;
;     NODRAW:       *IGL ADDITION* : Set this keyword to prevent the
;                   current window to be refreshed. The plot 
;                   is added to the window but will not be
;                   visible until the window is refreshed (by calling
;                   another IGL plotting command for example). This is
;                   useful is you wish to issue several plotting
;                   commands but want to display the results only at
;                   the end. 
;
;     POLAR:        Set this keyword to produce a polar plot.
;
;     PSYM:         An integer specifiying the plotting symbol to
;                   use. All the symbols from the SYMCAT
;                   routine (Coyote Library) are directly available. The
;                   default is !IGL_P.psym
;
;     SYMSIZE:      The size of the symbols when PSYM is set
;                   (float). The default is !IGL_P.symsize
;
;     THICK:        The thickness of the line (integer). The default
;                   is !IGL_P.thick
;    
; PACKAGE:
;
;    Interactive Graphics Library (IGL)
;
; AUTHORS:
;
;        RAPHAEL SADOUN
;        Graduate Student
;        Institut d'Astrophysique de Paris
;        98 bis boulevard Arago
;        75014 PARIS FRANCE
;        E-mail: raphael.sadoun@gmail.com
;
;
; MODIFICATION HISTORY:
;
;     Written by: Raphael Sadoun, 24 April 2012
;-
;*****************************************************************************************************
PRO EOPLOT, x, y, $
            AUTOSCALE=autoscale, $
            COLOR=color, $
            LINESTYLE=linestyle, $
            NOCLIP=noclip, $
            NODRAW=nodraw, $
            POLAR=polar, $
            PSYM=psym, $
            SYMSIZE=symsize, $
            THICK=thick

  COMPILE_OPT idl2, HIDDEN
  ON_ERROR, 2

  ;; Initialize the global system variables
  IGL_InitSysVars

  ;; Check positional arguments
  CASE N_PARAMS() OF
     
     0: Message, 'No arguments passed to eOPLOT'
     
     1: BEGIN
        ydata = x
        xdata = Findgen(N_Elements(ydata))
     END
     
     2: BEGIN
        xdata = x
        ydata = y
     END

     ELSE:
     
  ENDCASE

  ;; Make sure both arguments have same number of elements
  IF N_Elements(xdata) NE N_Elements(ydata) THEN Message, 'Input arguments must have same number of elements'

  ;; Default keyword values
  IF N_Elements(color) EQ 0 THEN color = !IGL_P.color
  IF N_Elements(linestyle) EQ 0 THEN linestyle = !IGL_P.linestyle
  IF N_Elements(noclip) EQ 0 THEN noclip = !IGL_P.noclip
  IF N_Elements(psym) EQ 0 THEN psym = !IGL_P.psym
  IF N_Elements(symsize) EQ 0 THEN symsize = (!IGL_P.symsize LE 0) ? 1 : !IGL_P.symsize
  IF N_Elements(thick) EQ 0 THEN thick = (!IGL_P.thick LE 0) ? 1 : !IGL_P.thick

  ;; Get the current IGL window
  currentWindow = IGL_GetCurrentWindow()
  IF OBJ_ISA_VALID(currentWindow,'IGL_Window') EQ 0 THEN Message, 'No valid IGL window available'

  ;; Get the current coordinate system
  currentCoords = IGL_GetSysVars('currentCoord')
  IF OBJ_ISA_VALID(currentCoords,'CatCoord') EQ 0 THEN Message, 'A coordinate system is not established'

  ;; Get the object that represents the coordinate system in the graphic
  currentCoords -> GetProperty, FIRST_PARENT=parent
  IF OBJ_VALID(parent) EQ 0 THEN Message, 'Invalid coordinate system'

  ;; Get the graphic in which the parent object is contained
  parent -> GetProperty, FIRST_PARENT=graphic
  IF OBJ_ISA_VALID(graphic,'IGL_Graphic') EQ 0 THEN message, 'Current coordinate system is not in a valid graphic'

  ;; Create a new plot 
  plotObject = obj_new('IGL_Plot', $
                       XDATA=xdata, $
                       YDATA=ydata, $
                       COLOR=color, $
                       LINESTYLE=linestyle, $
                       CLIP=(1-Keyword_Set(noclip)), $
                       POLAR=Keyword_set(polar), $
                       PSYM=psym, $
                       SYMSIZE=symsize, $
                       THICK=thick $
                      )
  plotObject -> SetProperty, Name='Oplot', Coord_Object=currentCoords

  ;; Add the plot to the object that holds the coordinate system
  parent -> Add, plotObject

  ;; Do we need to autoscale ?
  IF Keyword_Set(autoscale) THEN graphic -> ZoomOut, /AllTheWay, /Nodraw

  ;; Redraw window's content
  IF Keyword_set(nodraw) EQ 0 THEN currentWindow -> DrawWindow, /Buffered_Output

  ;; Refresh property editor
  currentWindow -> GetProperty, PropertyPanel=PropertyPanel
  IF OBJ_VALID(PropertyPanel) THEN PropertyPanel -> UpdateTreeBrowser

END
